package Client;

import GUI.RegisterFrame;
import GUI.showMessageFrame;
import Utils.PacketUtils;

import javax.swing.*;
import java.io.File;
import java.net.*;

import static java.lang.System.exit;
import static java.lang.Thread.sleep;

/**
 * The type Client.
 *
 * @author dzr
 * @version 1.0
 */
public class Client{
    /**
     * The server's port.
     */
    public static final int server_port = 8888;
    /**
     * The server's host_name.
     */
    public static String host_name;
    /**
     * The client's socket.
     */
    public static DatagramSocket client;
    /**
     * The server's address.
     */
    public static InetSocketAddress server;
    /**
     * The Program's rootDir.
     */
    public static File rootDir = new File("C:" + File.separator + "featherChat");

    /**
     * Start.
     *
     * @throws SocketException      the socket exception
     * @throws UnknownHostException the unknown host exception
     */
    public static void start() throws SocketException, UnknownHostException {
        host_name = "82.156.207.144";
        server = new InetSocketAddress(host_name, server_port);
        try{
            client = new DatagramSocket(8087);
            PacketUtils.client = client;
            if (!rootDir.exists()){
                rootDir.mkdirs();
            }
        }catch (BindException e){
            JOptionPane.showMessageDialog(
                    new showMessageFrame(""),
                    "该机器已经登录，请退出后重试",
                    "提示",
                    JOptionPane.WARNING_MESSAGE
            );
            exit(0);
        }
    }
}
